package com.gozap.base;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class MethodTest {
	public static void main(String[] args) {
		MethodTest test = new MethodTest();
		test.getProcess();
		System.out.println(test);
	}
	
	public void killProcessByName(String processName) {
		try {
			String commandPre = "cmd /c ";
			String commandsuf = "taskkill /IM " + processName + " /t /f";
			String command = commandPre + commandsuf;

			Runtime.getRuntime().exec(command);

		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("there is not have the process named :"
					+ processName);
		}
	}
	public void killProcessByPid(String pid) {
		try {
			String commandPre = "cmd /c ";
			String commandsuf = "taskkill /pid " + pid + " /t /f";
			String command = commandPre + commandsuf;

			Runtime.getRuntime().exec(command);

		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("there is not have the pid :"
					+ pid);
		}
	}

	public void getProcess() {
		try {
			String commandPre = "cmd /c ";
			String commandsuf = "tasklist";
			String command = commandPre + commandsuf;
			Process process = Runtime.getRuntime().exec(command);
			InputStream stream = process.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					stream, "gbk"));
			String temp;
			while ((temp = reader.readLine()) != null) {
				System.out.println(temp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
